﻿using UnityEngine;
namespace HBFramework
{
    /// <summary>
    /// FPS工具,获取当前帧率
    /// </summary>
    public class FPSManager : Singleton<FPSManager>, ISingleton, ISingletonUpdate
    {
        int ISingletonUpdate.Order => 100;
        /// <summary>
        /// 帧率
        /// </summary>
        public int FPS { get; private set; } = 0;
        /// <summary>
        /// 刷新间隔
        /// </summary>
        public float RefreshInterval
        {
            get => refreshInterval;
            set
            {
                if (value <= 0.1f)
                {
                    Debug.LogError("[FPSManager] 刷新间隔不能小于0.1秒");
                }
                else
                {
                    refreshInterval = value;
                }
            }
        }

        private float curSeconds = 0;//当前时间
        private int fps;//当前帧数
        private float refreshInterval = .1f;//刷新间隔

        void ISingleton.OnInit()
        {
            Debug.Log("[FPSManager] 开始帧率计算");
        }

        void ISingletonUpdate.OnUpdate()
        {
            curSeconds += Time.deltaTime;
            if (curSeconds >= refreshInterval)
            {
                FPS = (int)(fps / refreshInterval);
                fps = 0;
                curSeconds = 0;
            }
            else
                fps += 1;
        }

        void ISingleton.OnDestroy()
        {

        }
    }
}